#include "mutex.h"

GMutex::GMutex()
{
#ifdef WIN32
	InitializeCriticalSection(&m_Lock);
#else
	pthread_mutexattr_t attr;
	pthread_mutexattr_init(&attr);
	pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
	pthread_mutex_init(&m_crit, &attr);
	pthread_mutex_destroy(&attr);
#endif
}

GMutex::~GMutex()
{
#ifdef WIN32
	DeleteCriticalSection(&m_Lock);
#else
	pthread_mutexattr_destroy(&m_Lock);
#endif
}

void GMutex::Lock()
{
#ifdef WIN32
	EnterCriticalSection(&m_Lock);
#else
	pthread_mutex_lock(&m_Lock);
#endif
}

void GMutex::UnLock()
{
#ifdef WIN32
	LeaveCriticalSection(&m_Lock);
#else
	pthread_mutex_unlock(&m_Lock);
#endif
}

GAutoMutex::GAutoMutex()
{
	m_mutex.Lock();
}

GAutoMutex::~GAutoMutex()
{
	m_mutex.UnLock();
}
